/**   
* @Title: CaiWuXiangGuanController.java 
* @Package com.zjdiepu.www.manage.api.zpp.controller 
* @author zhangpp
* @date 2017年5月31日 下午5:34:07 
* @version V1.0   
*/
package com.zjdiepu.www.manage.api.zpp.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import springfox.documentation.annotations.ApiIgnore;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.annotation.Update;
import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.core.PageVo;
import com.zjdiepu.www.base.enums.type.DataDicType;
import com.zjdiepu.www.common.util.CollectionInitUtil;
import com.zjdiepu.www.common.util.ZhppDateUtil;
import com.zjdiepu.www.commpent.mapper.yw.AbstractYwBusinessLoanVoNewMapper;
import com.zjdiepu.www.commpent.mapper.yw.YwBusinessCarVoMapper;
import com.zjdiepu.www.dao.zpp.PublicDao;
import com.zjdiepu.www.ibiz.cache.ICachedDictionaryService;
import com.zjdiepu.www.ibiz.cw.ICwRebateService;
import com.zjdiepu.www.ibiz.dh.IDhDepositMoneyService;
import com.zjdiepu.www.ibiz.dh.IDhSettleService;
import com.zjdiepu.www.ibiz.dz.IDzLendingService;
import com.zjdiepu.www.ibiz.fk.IFkAuditListService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessCarService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessLoanService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessService;
import com.zjdiepu.www.ibiz.zpp.CaiWuXiangGuanService;
import com.zjdiepu.www.ibiz.zpp.PublicService;
import com.zjdiepu.www.ibiz.zpp.SystemService;
import com.zjdiepu.www.model.cw.dto.CwRebate;
import com.zjdiepu.www.model.dh.dto.DhDepositMoney;
import com.zjdiepu.www.model.dh.dto.DhSettle;
import com.zjdiepu.www.model.fk.dto.FkAuditList;
import com.zjdiepu.www.model.yw.dto.YwBusiness;
import com.zjdiepu.www.model.yw.dto.YwBusinessCar;
import com.zjdiepu.www.model.yw.dto.YwBusinessLoan;
import com.zjdiepu.www.model.yw.ext.YwBusinessCarExt;
import com.zjdiepu.www.model.yw.ext.YwBusinessLoanExt;
import com.zjdiepu.www.model.zpp.cwxg.vo.DengJiYwTiChengVo;
import com.zjdiepu.www.model.zpp.cwxg.vo.TuiYaJinListVo;
import com.zjdiepu.www.model.zpp.pub.ext.ParamsExt;
import com.zjdiepu.www.model.zpp.pub.ext.WhereSqlExt;

/**      
 * 项目名称：diepu-parent-manage   
 * 类名称：CaiWuXiangGuanController   
 * 类描述：   
 * 创建人：zhangpp
 * 创建时间：2017年5月31日 下午5:34:07   
 * 修改人：zhangpp
 * 修改时间：2017年5月31日 下午5:34:07   
 * 修改备注：   
 * @version V1.0     
 */
@RestController
@RequestMapping(value= "CaiWuXiangGuanController", produces="application/json;charset=UTF-8")
@Api(tags = {"作者：张鹏鹏，电话：15158822934"}, produces="application/json;charset=UTF-8")
public class CaiWuXiangGuanController extends BaseController
{

	@Resource
	private CaiWuXiangGuanService caiWuXiangGuanService;
	
	@Resource
    private IDhDepositMoneyService dhDepositMoneyService;
	
	@Resource
	private ICachedDictionaryService iCachedDictionaryService; 
	
	@Resource
	private IDhSettleService iDhSettleService;
	
	@Resource
	private IYwBusinessService iYwBusinessService;
	
    @Resource
    private YwBusinessCarVoMapper ywBusinessCarVoMapper;
    
    @Resource
    private IYwBusinessCarService ywBusinessCarService;
    
    @Resource
    private IYwBusinessLoanService ywBusinessLoanService;
	
    @Resource
    private AbstractYwBusinessLoanVoNewMapper ywBusinessLoanVoMapper;
    
    @Resource
    private IYwBusinessService ywBusinessService;
    
    @Resource
    private ICwRebateService cwRebateService;
    
    @Resource
    private IFkAuditListService fkAuditListService; 
    
    @Resource
    private IDzLendingService dzLendingService;
    
	@Resource
	private PublicDao publicDao;
	
	@Resource
	private PublicService publicService;
	
	@Resource
	private SystemService systemService;
	
	/**
	*  客户申请结清
	*  @author  zhangpp
	*  @Title:  diepuKeHuJiQing  
	*    
	*  @param  @param dhSettle
	*  @param  @return        设定文件  
	*  @return  Map<String,Object>        返回类型  
	*  @version  V1.0
	*  @throws
	 */
	@RequestMapping(value = "diepuKeHuJiQing", method = RequestMethod.POST)
    public Map<String, Object> diepuKeHuJiQing(DhSettle dhSettle) 
    {
		AbstractSysUser currentUser = getCurrentUser();
    	Map<String, Object> maps = success();
    	caiWuXiangGuanService.diepuKeHuJiQing(currentUser, dhSettle);
		return maps;
	}
	
	/**
	*  登记业务提成
	*  @author  zhangpp
	*  @Title:  ywTiChengSave  
	*  @param  @param cwRebate
	*  @param  @return        设定文件  
	*  @return  Map<String,Object>        返回类型  
	*  @version  V1.0
	*  @throws
	 */
	@RequestMapping(value = "ywTiChengSave", method = RequestMethod.POST)
    public Map<String, Object> ywTiChengSave(CwRebate cwRebate) 
    {
		AbstractSysUser currentUser = getCurrentUser();
    	Map<String, Object> maps = success();
    	caiWuXiangGuanService.diepuYwTiChengSave(currentUser, cwRebate);
		return maps;
	}
	
	/**
	*  如果银行放款有值  则隐藏保存按钮
	*  @author  zhangpp
	*  @Title:  getBankFangkuanCount  
	*  @param  @param busiId
	*  @param  @return        设定文件  
	*  @return  Map<String,Object>        返回类型  
	*  @version  V1.0
	*  @throws
	 */
	@RequestMapping(value = "getBankFangkuanCount", method = RequestMethod.POST)
    public Map<String, Object> getBankFangkuanCount(String busiId) 
    {
    	Map<String, Object> maps = success();
    	
    	//银行放款
    	String sqlXmls = "select t.id from dz_lending t where t.busi_id = '"+busiId+"' and t.dp_status = 1";
    	List<Map<String, String>> list = publicService.queryWhereSql(sqlXmls);
    	if(null != list && 0 != list.size())
    	{
    		maps.put("dzLendingCount", list.size());
    		return maps;
    	}
    	maps.put("dzLendingCount", 0);
		return maps;
	}
	
	
	/**
	*  业务进城
	*  @author  zhangpp
	*  @Title:  getYwJinCheng  
	*  @param  @param busiId
	*  @param  @return        设定文件  
	*  @return  Map<String,Object>        返回类型  
	*  @version  V1.0
	*  @throws
	 */
	@RequestMapping(value = "getYwJinCheng", method = RequestMethod.POST)
    public Map<String, Object> getYwJinCheng(String busiId) 
    {
    	Map<String, Object> maps = success();
    	
    	//查询业务信息表
    	YwBusiness ywBusiness = ywBusinessService.selectById(busiId);
    	maps.put("ywBusiness", ywBusiness);
    	
    	//返利表
    	CwRebate cwRebate = new CwRebate();
    	cwRebate.setBusiId(busiId);
    	cwRebate.setDpStatus(1);
    	cwRebate = cwRebateService.selectRefined(cwRebate);
    	maps.put("cwRebate", cwRebate);
    	
    	//审核 大额审批
    	FkAuditList fkAuditList = new FkAuditList();
    	fkAuditList.setBusiId(busiId);
    	fkAuditList.setModeId("M_dedksp");
    	fkAuditList.setDpStatus(1);
    	fkAuditList = fkAuditListService.selectRefined(fkAuditList);
    	maps.put("fkAuditList", fkAuditList);
    	
		return maps;
	}
	
	/**
	*  车辆信息
	*  @author  zhangpp
	*  @Title:  dakuanDetail  
	*  @param  @param busiId
	*  @param  @return        设定文件  
	*  @return  Map<String,Object>        返回类型  
	*  @version  V1.0
	*  @throws
	 */
    @RequestMapping(value = "dakuanDetail", method = RequestMethod.POST)
    public Map<String, Object> dakuanDetail(String busiId) {
    	Map<String, Object> maps = success();
    	// 查询车辆&车贷信息
    	YwBusinessCarExt ywBusinessCarExt = new YwBusinessCarExt();
		ywBusinessCarExt.setBaseWhere();
		ywBusinessCarExt.setRows(1);// 只能返回一条数据
		ywBusinessCarExt.setBusiId(busiId);
    	List<YwBusinessCar> ywBusinessCars = ywBusinessCarService.selectByExt(ywBusinessCarExt);
		if (CollectionUtils.isNotEmpty(ywBusinessCars)) {
			YwBusinessCar ywBusinessCar = ywBusinessCars.get(0);
			ywBusinessCarVoMapper.mapper(ywBusinessCar);
			maps.put("ywBusinessCar", ywBusinessCar);
			
			String carId = ywBusinessCar.getId();
			// 查询车辆&车贷信息 
			YwBusinessLoanExt ywBusinessLoanExt = new YwBusinessLoanExt();
			ywBusinessLoanExt.setBaseWhere();
			ywBusinessLoanExt.setRows(1);// 只能返回一条数据
			ywBusinessLoanExt.setBusiId(busiId);
			ywBusinessLoanExt.setCarId(carId);
			List<YwBusinessLoan> ywBusinessLoans = ywBusinessLoanService.selectByExt(ywBusinessLoanExt);
			if (CollectionUtils.isNotEmpty(ywBusinessLoans)) {
				YwBusinessLoan ywBusinessLoan = ywBusinessLoans.get(0);
				ywBusinessLoanVoMapper.mapper(ywBusinessLoan);
				maps.put("ywBusinessLoan", ywBusinessLoan);
			}
		}
		return maps;
	}

	
	/**
	* 下拉列表
	* @author zhangpp
	* @Title: getDataDictionaryList 
	* @param @return    设定文件 
	* @return Map<String,Object>    返回类型 
	* @version V1.0
	* @throws
	 */
	@RequestMapping(value="getDataDictionaryList",method = RequestMethod.POST, produces="application/json;charset=UTF-8")
	public Map<String, Object> getDataDictionaryList(String busiId)
	{
		Map<String, Object> maps = success();
		
		//财务相关/结清单确认
		Map<String, String> caiwujieqing_result = iCachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.CAIWU_XIANGGUAN_BANK_TUIJIAN,"请选择...");
		maps.put("caiwujieqing_result", caiwujieqing_result);
		
		DhSettle dhSettle = new DhSettle();
		dhSettle.setBusiId(busiId);
		dhSettle.setDpStatus(1);
		dhSettle = iDhSettleService.selectRefined(dhSettle);
		maps.put("dhSettle", dhSettle);
        return maps;
	}
	
	/**
	* 财务相关  客户退押金
	* @author zhangpp
	* @Title: modify 
	* @param @param dhDepositMoney
	* @param @return    设定文件 
	* @return ApiResponse    返回类型 
	* @version V1.0
	* @throws
	 */
	@ApiOperation(value = "修改", response = Boolean.class, httpMethod="POST", tags={"修改"})
    @RequestMapping(value = "modify", method = RequestMethod.POST)
    public ApiResponse modify(@Validated(Update.class) @ModelAttribute DhDepositMoney dhDepositMoney) {
    	AbstractSysUser currentUser = getCurrentUser();
    	caiWuXiangGuanService.diepuTuiYaPriceUp(currentUser, dhDepositMoney);
        return ApiResponse.success(true);
    }
	 
	/**
	* 财务相关  客户退押金
	* @author zhangpp
	* @Title: caiWuXiangGuanKeHuTuiYaJin 
	* 
	* @param @param paramsExt
	* @param @return    设定文件 
	* @return Map<String,Object>    返回类型 
	* @version V1.0
	* @throws
	 */
	@RequestMapping(value="caiWuXiangGuanKeHuTuiYaJin",method = RequestMethod.POST, produces="application/json;charset=UTF-8")
	public Map<String, Object> caiWuXiangGuanKeHuTuiYaJin(@ApiIgnore ParamsExt paramsExt)
	{
		Map<String, Object> maps = success();
		String busiId = paramsExt.getBusiId();
		
		//业务表数据
		YwBusiness ywBusiness = iYwBusinessService.selectById(busiId);
		maps.put("ywBusiness", ywBusiness);
		
		//收支管理
		WhereSqlExt whereSqlExt = new WhereSqlExt();
		String whereSql = "select t.id,t.item_code,t.amount_item from cw_income_expenses_item t where t.busi_id = '"+busiId+"' and t.dp_status = 1";
		whereSqlExt.setWhereSql(whereSql);
		List<Map<String, String>>  cwIncomeExpensesItem = publicDao.selectWhereSql(whereSqlExt);
		maps.put("cwIncomeExpensesItem", cwIncomeExpensesItem);
		
		
		DhDepositMoney dhDepositMoney = dhDepositMoneyService.selectById(paramsExt.getId());
		dhDepositMoney.setTypeRemit(iCachedDictionaryService.getName(dhDepositMoney.getTypeRemit(), DataDicType.SETTLEMENT_TYPE));
		maps.put("dhDepositMoney", dhDepositMoney) ;
		
		Map<String, String> gongsiCarad = iCachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.GONGSI_CARAD,"");
		gongsiCarad.remove("");
		maps.put("gongsiCarad", gongsiCarad);
		return maps;
	}
	
	/**
	* 财务相关   客户退押金
	* @author zhangpp
	* @Title: list 
	* @param @param dhDepositMoneyExt
	* @param @return    设定文件 
	* @return ApiResponse    返回类型 
	* @version V1.0
	* @throws
	 */
	@ApiOperation(value = "条件查询",response = DhDepositMoney.class, httpMethod="POST", tags={"列表"})
    @RequestMapping(value = "getTuiYaJinList", method = RequestMethod.POST)
    public Map<String, Object> getTuiYaJinList(@ModelAttribute ParamsExt paramsExt) throws Exception
    {
		
		Map<String, Object> maps = success();
		
		String whereSql = " and t.status_refund_deposit = 100 or t.status_refund_deposit = 50 ";
		
		if(null != paramsExt)
		{
			String customerName = paramsExt.getCustomerName();
			String salesmanName = paramsExt.getSalesmanName();
			
			if(StringUtils.isNotBlank(customerName))
			{
				whereSql += " and p.name  like '%"+customerName+"%' ";
			}
			
			if(StringUtils.isNotBlank(salesmanName))
			{
				whereSql += " and y.name like '%"+salesmanName+"%' ";
			}
			
			String submitTimeStart = paramsExt.getSubmitTimeStart();
			String submitTimeEnd = paramsExt.getSubmitTimeEnd();
			
			if((null != submitTimeStart && !("").equalsIgnoreCase(submitTimeStart)) && (null != submitTimeEnd && !("").equals(submitTimeEnd)))
			{
				int start = ZhppDateUtil.dateConvertTimeStamp(submitTimeStart, "yyyy-MM-dd");
				int end =  ZhppDateUtil.dateConvertTimeStamp(submitTimeEnd, "yyyy-MM-dd");
				whereSql += " and t.created >= "+start + " and t.created <= "+ (end + 86400);
			}
			else
			{
				if(null != submitTimeStart && !("").equals(submitTimeStart))
				{
					int start = ZhppDateUtil.dateConvertTimeStamp(submitTimeStart, "yyyy-MM-dd");
					whereSql += " and t.created >= "+start;
				}
				else if(null != submitTimeEnd && !("").equals(submitTimeEnd))
				{
					int end =  ZhppDateUtil.dateConvertTimeStamp(submitTimeEnd, "yyyy-MM-dd");
					whereSql += " and t.created <= "+(end + 86400);
				}
			}
		}
		
		//登录用户银行范围查询
//		SysUser sysUser =(SysUser)getCurrentUser();
//		String lBank = sysUser.getLoanBank();
//		whereSql += systemService.getBankSqlXmls(lBank, "t.bank");
	    //登录用户银行范围查询
		
		paramsExt.setWhereSql(whereSql);
		paramsExt.notDeleted();
        PageVo page = getPage(paramsExt);
        int records = caiWuXiangGuanService.getTuiYaJinCount(paramsExt);
        page.setRecords(records);
        if (records > 0) 
        {
        	paramsExt.setOrderSql(" t.created desc ");
        	List<TuiYaJinListVo> dhDepositMoneys = caiWuXiangGuanService.getTuiYaJinList(paramsExt);
            for(int i=0;i<dhDepositMoneys.size();i++)
            {
            	TuiYaJinListVo dp = dhDepositMoneys.get(i);
            	dp.setTypeRemit(iCachedDictionaryService.getName(dp.getTypeRemit(), DataDicType.SETTLEMENT_TYPE));
            	dp.setBankName(iCachedDictionaryService.getName(dp.getBank(), DataDicType.LOAN_BANK));
            }
            page.setRows(dhDepositMoneys);
        } 
        else 
        {
            page.setRows(CollectionInitUtil.emptyList());
        }
        maps.put("result", page);
        return maps;
    }
	
	/**
	*  登记业务提成
	*  @author  zhangpp
	*  @Title:  getDengJiYwTiChengCount  
	*    
	*  @param  @param paramsExt
	*  @param  @return
	*  @param  @throws Exception        设定文件  
	*  @return  Map<String,Object>        返回类型  
	*  @version  V1.0
	*  @throws
	 */
	@RequestMapping(value = "getDengJiYwTiChengCount", method = RequestMethod.POST)
    public Map<String, Object> getDengJiYwTiChengCount(@ModelAttribute ParamsExt paramsExt) throws Exception
    {
		Map<String, Object> maps = success();
		
		String whereSql = " and v.busi_status >= 50 and v.busi_status < 280 ";
		
		String dayEnum = paramsExt.getDayEnum();
		
		if(null != dayEnum && !("").equals(dayEnum))
		{
			int stamp = ZhppDateUtil.dateConvertStamp();
			
			if(dayEnum.equals("1"))
			{
				whereSql += " and v.created >= "+stamp + " and v.created < "+(stamp + 86400);
			}
			else if(dayEnum.equals("2"))
			{
				whereSql += " and v.created < "+stamp + " and v.created >= "+(stamp - 86400 * 2);  
			}
			else if(dayEnum.equals("3"))
			{
				whereSql += " and v.created > (unix_timestamp() - 3600 * 24 * 7) ";
			}
		}
		
		String submitTimeStart = paramsExt.getSubmitTimeStart();
		String submitTimeEnd = paramsExt.getSubmitTimeEnd();
		
		if((null != submitTimeStart && !("").equalsIgnoreCase(submitTimeStart)) && (null != submitTimeEnd && !("").equals(submitTimeEnd)))
		{
			int start = ZhppDateUtil.dateConvertTimeStamp(submitTimeStart, "yyyy-MM-dd");
			int end =  ZhppDateUtil.dateConvertTimeStamp(submitTimeEnd, "yyyy-MM-dd");
			whereSql += " and v.created >= "+start + " and v.created <= "+ (end + 86400);
		}
		else
		{
			if(null != submitTimeStart && !("").equals(submitTimeStart))
			{
				int start = ZhppDateUtil.dateConvertTimeStamp(submitTimeStart, "yyyy-MM-dd");
				whereSql += " and v.created >= "+start;
			}
			else if(null != submitTimeEnd && !("").equals(submitTimeEnd))
			{
				int end =  ZhppDateUtil.dateConvertTimeStamp(submitTimeEnd, "yyyy-MM-dd");
				whereSql += " and v.created <= "+(end + 86400);
			}
		}
		
		paramsExt.setWhereSql(whereSql);
		PageVo page = getPage(paramsExt);
        int records = caiWuXiangGuanService.getDengJiYwTiChengCount(paramsExt);
        page.setRecords(records);
        if (records > 0) {
        	List<DengJiYwTiChengVo> list = caiWuXiangGuanService.getDengJiYwTiChengVo(paramsExt);
        	for(int i=0;i<list.size();i++)
        	{
        		DengJiYwTiChengVo dengJiYwTiChengVo = list.get(i);
        		dengJiYwTiChengVo.setChannelType(iCachedDictionaryService.getName(dengJiYwTiChengVo.getChannelType(), DataDicType.CHANNEL_TYPE));
        		dengJiYwTiChengVo.setBankLoan(iCachedDictionaryService.getName(dengJiYwTiChengVo.getBankLoan(), DataDicType.LOAN_BANK));
        	}
            page.setRows(list);
        } else {
            page.setRows(CollectionInitUtil.emptyList());
        }
        maps.put("result", page);
        return maps;
    }

	 
	/**
	* 财务相关  客户结清
	* @author zhangpp
	* @Title: caiWuXiangGuanKeHuJieQing 
	* 
	* @param @param dhSettle
	* @param @param ywBusiness
	* @param @return    设定文件 
	* @return ApiResponse    返回类型 
	* @version V1.0
	* @throws
	 */
	@RequestMapping(value="caiWuXiangGuanKeHuJieQing",method = RequestMethod.POST, produces="application/json;charset=UTF-8")
	public ApiResponse caiWuXiangGuanKeHuJieQing(@ApiIgnore DhSettle dhSettle,YwBusiness ywBusiness)
	{
		AbstractSysUser currentUser = getCurrentUser();
		caiWuXiangGuanService.diepuCaiWuXiangGuanKeHuJieQing(dhSettle, ywBusiness, currentUser);
		return ApiResponse.success(true);
	}
}
